﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CPPEI.Coolzon.UI;
using CPPEI.Coolzon.Metadata;
using System.Collections;
using CPPEI.Coolzon.UI.Data;

namespace CPPEI.Coolzon.BrowserApp.MainViewControl
{
    /// <summary>
    /// RecentVisit.xaml 的交互逻辑
    /// </summary>
    public partial class RecentVisit : UserControl
    {
        public Solution _solution;
        public ArrayList RecentList = new ArrayList();
        public string openedframeName = string.Empty;
        public string openedsceneName = string.Empty;
        public string lastopenedsceneName = string.Empty;
        public RecentVisit()
        {
            InitializeComponent();
        }

        void tb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Scene scene = (sender as TextBlock).DataContext as Scene;
            Event.Core.CoreEventBus.Publish(new Event.Core.CoreOpenSceneEvent(SwitchMode.Replace, "", scene.Name, null));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            UI.ScenePart scenePart = RemoteEventProxy.Instance.RenderView.GetCurrentPart() as UI.ScenePart;
            Event.Core.CoreEventBus.Publish(new Event.Core.CoreShowQueryPanelEvent(scenePart, scenePart.SceneMatadata));
        }

        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            btnBack.Visibility = System.Windows.Visibility.Collapsed;

            UI.ScenePart scenePart = RemoteEventProxy.Instance.RenderView.GetCurrentPart() as UI.ScenePart;
            //Common.EventBus.Instance.Publish(new UI.Event.CloseSceneEvent(SwitchMode.Replace, openedframeName, openedsceneName));
            Event.Core.CoreEventBus.Publish(new Event.Core.CoreCloseSceneEvent(SwitchMode.Replace, openedframeName, openedsceneName));

            Scene scene = SolutionUtil.GetScene(_solution, lastopenedsceneName);
            SetPositionStr(scene);
        }

        #region implement ISceneDisplayChangedEventReceiver

        [Common.Subscribe]
        public void OnOpened(Event.Core.CoreOpenSceneCompletedEvent @event)
        {
            if (@event.SwitchMode == SwitchMode.Pop)
            {
                return;
            }
            lastopenedsceneName = openedsceneName;
            openedframeName = @event.FrameName;
            openedsceneName = @event.SceneName;

            Scene scene = SolutionUtil.GetScene(_solution, @event.SceneName);
            if (!scene.IsShowInNavigation)
            {
                btnBack.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                btnBack.Visibility = System.Windows.Visibility.Collapsed;
            }

            if (scene.IsShowQueryPanel)
            {
                btnSearch.Visibility = Visibility.Visible;
            }
            else
            {
                btnSearch.Visibility = Visibility.Collapsed;
            }

            SetPositionStr(scene);
        }

        [Common.Subscribe]
        public void OnHidden(Event.Core.CoreCloseSceneCompletedEvent @event)
        {
        }

        #endregion

        private void SetPositionStr(Scene scene)
        {
            spLayout.Children.Clear();
            ArrayList al = _solution.GetScenePath(scene);
            if (al.Count > 0)
            {
                tbPosition.Text = "当前位置-";
            }
            for (int i = 0; i < al.Count; i++)
            {
                Scene node = al[i] as Scene;
                TextBlock tb = new TextBlock();
                tb.Style = (Style)spPosition.Resources["lblBase"];
                tb.Text = node.Caption;
                tb.MouseLeftButtonDown += tb_MouseLeftButtonDown;
                tb.DataContext = node;
                spLayout.Children.Add(tb);

                if (i < al.Count - 1)
                {
                    TextBlock tbSep = new TextBlock();
                    tbSep.Style = (Style)spPosition.Resources["lblBase"];
                    tbSep.Text = "/";
                    spLayout.Children.Add(tbSep);
                }
            }
        }
    } 
}
